﻿using Caltech.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Caltech.Results;
using CaltechSoft.Service.CommandService;
using LinqKit;
using Microsoft.AspNet.Identity;
using ServiceModel = CaltechSoft.Service.DBCommand;
using AutoMapper;
using CaltechSoft.Service.ReportService;
using CaltechSoft.Service.SearchCriteria;
using CaltechSoft.Service.Repositories;
using CaltechSoft.Service.EntityFW.Entity;
using System.Linq.Expressions;
using Caltech.Extensions;
using Caltech.Service.Common;
using System.Threading.Tasks;

namespace Caltech.Controllers
{
    [HasAccess(Areas.QuoteView)]
    public class QuoteController : BaseApiController<Quote, QuoteModel, QuoteSearchModel>
    {
        public UserManager<User> UserRepository { get; private set; }

        public QuoteController(IRepository<Quote> repository, UserManager<User> userRepository)
            : base(repository)
        {
            UserRepository = userRepository;
        }

        public override IHttpActionResult Get()
        {
            var queryItems = Request.RequestUri.ParseQueryString();
            string customerName = queryItems["customerName"];
            string quoteNumber = queryItems["quoteNumber"];
            string salesman = queryItems["salesman"];
            int customerId;
            Expression<Func<Quote, bool>> filter = null;

            if (int.TryParse(queryItems["customerId"], out customerId))
            {
                filter = quote => quote.Company.Id == customerId;
            }
            else if (!string.IsNullOrEmpty(customerName))
            {
                filter = quote => quote.Company.Name.ToLower().Contains(customerName.ToLower());
            }
            else if (!string.IsNullOrEmpty(quoteNumber))
            {
                filter = quote => quote.QuoteNumber.ToLower().Contains(quoteNumber.ToLower());
            }

            if (!string.IsNullOrWhiteSpace(salesman))
            {
                var userIds = UserRepository.Users.Where(n => n.FirstName.Contains(salesman) || n.LastName.Contains(salesman)).Select(n => n.Id).ToList();
                if (filter != null)
                {
                    filter.And(quote => userIds.Contains(quote.Salesman.Id));
                }
                else
                {
                    filter = quote => userIds.Contains(quote.Salesman.Id);
                }
            }

            return QueryData(filter);
        }

        [HasAccess(Areas.QuoteCreate)]
        public override async Task<IHttpActionResult> Post(QuoteModel model)
        {
            return await base.Post(model);
        }

        [HasAccess(Areas.QuoteEdit)]
        public override async Task<IHttpActionResult> Put(QuoteModel model)
        {
            return await base.Put(model);
        }

        [HasAccess(Areas.QuoteDelete)]
        public override async Task<IHttpActionResult> Delete(int id)
        {
            return await base.Delete(id);
        }
    }
}